Uec3.IndividualClientWindow = Ext.extend(Ext.app.Module,{
	id: 'individualClient',
   /*
    * Initialize this module.
    * This function is called at startup (page load/refresh).
    */
   init : function(initParams){
   
   },
   
   getComponent:function(params){
   	      
   	      var userStatus_store = getDictionaryStore(Uec3.App.Constants.homeUrl,{group:'user_status'});
   	      var userStatus_comb = getDictionaryComboBox(userStatus_store,'local','status','用户状态','status');
   	
   	      var userSex_store = getDictionaryStore(Uec3.App.Constants.homeUrl,{group:'user_sex'});
   	      var userSex_comb = getDictionaryComboBox(userSex_store,'local','sex','性别','sex');
   	      
   		  var search_form = new Ext.FormPanel({
   	       		frame:true,
   	      		border:false,
   	       	    items:[
   	       	      {
   	       	        layout:'column',
   	       	        items:[
   	       	           {
   	       	             columnWidth:0.3,
   	       	             items:[
   	       	                {
   	       	                  layout:'form',
   	       	                  labelWidth:55,
   	       	                  items:[
   	       	                    {
   	       	                      xtype:'textfield',
   	       	                      fieldLabel:'用户邮箱',
   	       	                      name:'email',
   	       	                      width:60,
   	       	                      anchor:'100%'
   	       	                    }
   	       	                  ]
   	       	                }
   	       	             ]
   	       	           },{
   	       	             columnWidth:0.3,
   	       	             items:[
   	       	                {
   	       	                  layout:'form',
   	       	                  labelWidth:55,  	       	                  
   	       	                  items:[
   	       	                    {
   	       	                      xtype:'textfield',
   	       	                      fieldLabel:'用户姓名',
   	       	                      name:'name',  	       	                      
   	       	                      anchor:'100%'
   	       	                    }
   	       	                  ]
   	       	                }
   	       	             ]
   	       	           },{
   	       	              columnWidth:0.3,
   	       	              items:[
   	       	                 {
   	       	                   layout:'form',
   	       	                   labelWidth:55,
   	       	                   items:[userStatus_comb]
   	       	                 }
						  ]
   	       	           },{
   	                    xtype:'button',
				        text:'查找',
				        //style:{marginLeft:'15px',width:'75px'},
				        handler:searchUserByCondition
   	                 }
   	       	        ]
   	       	      }
   	            ]   	         
   	      });
   	      
   	      var userGrid_store = new Ext.data.JsonStore({
   	  			url:Uec3.App.Constants.homeUrl+"/user/userManage!individualClientBrowser.do",
   	  			root:'root',
   	  			totalProperty:'totalProperty',
   	  		    fields:['companyName','name','email','sex','status','memo','loginTimes',{name:'gmtCreate',type:'date',dateFormat:'Y-m-d H:i:s'},{name:'gmtLastLogin',type:'date',dateFormat:'Y-m-d H:i:s'}],
   	  		    listeners :{
                  'beforeload':function(){
                       Ext.MessageBox.wait("正在加载数据...","提示");
                   }            
                }
   	      });
   	     
   	     	//创建工具栏组件
		  var toolbar = new Ext.Toolbar({
		     items:[
			    {text : '发送激活邮件',iconCls : 'icon-add',itemId:'postEmail',handler:sendActiveEmail}			    	 
		     ]
		  });
   	      
          var column = new Ext.grid.ColumnModel({
          		columns:[
          		   	new Ext.grid.RowNumberer(),
      		 	   	{header:'公司名称',dataIndex:'companyName'},
      		 		{header:'用户姓名',dataIndex:'name'},
      		 		{header:'邮箱',dataIndex:'email'}, 
      		 		{header:'性别',dataIndex:'sex',width:50,renderer:Ext.util.Format.comboRenderer(userSex_comb)},
      		 		{header:'状态',dataIndex:'status',width:80,renderer:Ext.util.Format.comboRenderer(userStatus_comb)},
      		 		{header:'注册时间',dataIndex:'gmtCreate',renderer:Ext.util.Format.dateRenderer('Y-m-d H:i:s')},
      		 		{header:'最后登陆时间',dataIndex:'gmtLastLogin',renderer:Ext.util.Format.dateRenderer('Y-m-d H:i:s')},
      		 		{header:'登陆次数',dataIndex:'loginTimes'},     		 		
      		 		{header:'备注',dataIndex:'memo'}
          		],
          		defaults: {
          			menuDisabled: true         
       			}
          });
      		 
        
         var user_grid = new Ext.grid.GridPanel({
      		 viewConfig:{forceFit:true,scrollOffset:20},	   	   
	   	   	 autoScroll : true,
	   	   	 titleCollapse:true,
	         colModel:column,
	         height:250,
	         selModel:new Ext.grid.RowSelectionModel({moveEditorOnEnter:true,singleSelect :true}),
	         store:userGrid_store,	           
	         columnLines :true,
	         tbar:toolbar,
	         bbar: new Ext.PagingToolbar({
		            pageSize:19,
	                store:userGrid_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录",                   
                    listeners:{
                        beforechange:function(){
                      		 Ext.MessageBox.wait("正在加载数据...","提示");	                     
                        },
                        change:function(){
                            Ext.MessageBox.hide();
                        }
                    }
		     })
         });
         
         var join_panel= new Ext.Panel({
   	         border:false,
   	         
   	         items:[search_form,user_grid]
   	    });
   	    
         
         function searchUserByCondition(){
            var queryParam = Ext.encode(search_form.getForm().getValues());
            loadUser(queryParam);
         }
         
         function loadUser(queryParam){
         	 if(queryParam){
				 userGrid_store.baseParams={start:0,limit:19,queryParam:queryParam}; 
			 }else{
			 	 userGrid_store.baseParams={start:0,limit:19}; 				 
			 }
			 userGrid_store.load({callback:function(records,options,success){
	              if(success){
	                 var total=records.length;
	                 if(Ext.isEmpty(total)){
	                    Ext.MessageBox.alert("提示","未找到相关记录");
	                 }
	              }else{
	                 Ext.MessageBox.alert("提示","数据加载异常...");
	              }
		    }});
         }
         
         function sendActiveEmail(){
            var record = user_grid.getSelectionModel().getSelected();
            if(Ext.isEmpty(record)){
               Ext.MessageBox.alert("提示","请选择需要激活的用户!")
            }else{           
            	 if(record.data.status == 0){
               		Ext.MessageBox.confirm("提示","确定给此用户发送激活邮件?",function(btnId){           	   
            	 	     if(btnId=="yes"){
            	 	         sendEmail(record.data.email);
            	 	     }
            	    });
                 }else{
                    Ext.MessageBox.alert("提示","该用户已激活,无需发送激活邮件!")
                 }
            }
           
         }
         
         function sendEmail(email){       
                var myMask = new Ext.LoadMask(join_panel.ownerCt.el, {   
                    msg : "正在发送邮件,请稍后....."  
                });  
                myMask.show();        	   
                Ext.Ajax.request({		            
				      url:Uec3.App.Constants.homeUrl+"/user/userManage!sendActiveEmail.do",
				      method :"post",					   
				      params:{queryParam:email},
				      callback :function(options,success,response){
                 
                      },
                      success:function(response,options){
                      	 var resp = Ext.decode(response.responseText);
				         Ext.Msg.alert("提示",resp.msg);
				         myMask.hide();
                      },
                      failure:function(response,options){
                         Ext.Msg.alert("提示","发生异常,发送激活邮件失败!");
                         myMask.hide();
                      }
		      }); 
         }
         
        function init(){
   	       userSex_store.load();
   	       userStatus_store.load();   	       
   	       loadUser();
   	    } 
   	    
   	    init();
   	    
   	   
   	    return join_panel;
   },
   
   /**
	* Create this modules window here.
	*/
   createWindow : function(params){
    	var desktop = this.app.getDesktop();
        var win = desktop.getWindow('individualClient');
        if (!win) {
			win = desktop.createWindow({
				        id : "individualClient",
						title : '个人用户管理',
						width : 1000,
						height : 500,
						iconCls : 'acc-icon',
						maximizable : true,
						maximized:true,
						//autoScroll : true,
						constrainHeader : true,
						taskbuttonTooltip : "个人用户管理",
						//layout : "fit",
						items : [this.getComponent(params)]
					});
		}
		win.get(0).get(1).setHeight(win.getHeight()-70);
    	win.show(); 
        win.on('resize',function(win,width,height){
      	   win.get(0).get(1).setHeight(height-72);
        });  
        /*win.on('show',function(){
            win.add(this.getComponent(params));
            //win.get(0).get(1).setHeight(win.getHeight()-70);
        });*/
   }
});